package com.bjt.wms.sys.service;

import cn.hutool.core.date.DateUtil;
import com.bjt.common.config.paragetter.BasePageRequest;
import com.bjt.utils.R;
import com.bjt.wms.sys.model.base.LogLogin;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;

public class LogService {

    /**
     * 新增登陆日志
     *
     * @param username
     * @param ip
     * @param bResult
     * @param message
     */
    public void save(String username, String ip, boolean bResult, String... message) {
        LogLogin logLogin = new LogLogin();
        logLogin.setUsername(username);
        logLogin.setCreateTime(DateUtil.date().toString("yyyy-MM-dd HH:mm:ss"));
        logLogin.setIp(ip);
        logLogin.setResult(bResult ? "ok" : "failed");
        logLogin.setMessage(message.length > 0 ? message[0] : "");
        logLogin.save();
    }

    /**
     * 查询登陆日志
     *
     * @param basePageRequest
     * @param cond
     * @return
     */
    public R queryLogLoginList(BasePageRequest<LogLogin> basePageRequest, Kv cond) {
        SqlPara sqlPara = Db.getSqlPara("sys.log.queryLogLoginList", Kv.by("cond", cond));

        Page<Record> pageData = Db.paginate(basePageRequest.getPage(), basePageRequest.getLimit(), sqlPara);
        return R.ok().put("data", pageData.getList()).put("count", pageData.getTotalRow());
    }

    /**
     * 查询操作日志
     *
     * @param basePageRequest
     * @param cond
     * @return
     */
    public R queryLogOperateList(BasePageRequest<LogLogin> basePageRequest, Kv cond) {

        return R.ok().put("count", 0);
    }

    /**
     * 查询系统日志
     *
     * @param basePageRequest
     * @param cond
     * @return
     */
    public R queryLogSysList(BasePageRequest<LogLogin> basePageRequest, Kv cond) {

        return R.ok().put("count", 0);
    }
}
